package boogui.movie;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import boogui.member.UserVO;

public class S_MovieSeatDialog extends JDialog {
	
	private MovieVO m = null;
	private UserVO u = null;
//	private MovieService ms = null;

	static BtnActionListener al = null;	
	private MovieReserveDAO bd = new MovieReserveDAO();
	
	private static S_MovieSeatDialog p = null;
	
	static int as = 0;

	private JCheckBox seats[][];
	private JLabel label[];	

	private char isSelec;	

	private static String updateSeatSQL;

	private final JPanel contentPanel = new JPanel();

	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		try {
			S_MovieSeatDialog dialog = new S_MovieSeatDialog(new MovieVO());
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
*/
	/**
	 * Create the dialog.
	 */
	public S_MovieSeatDialog(MovieVO m, UserVO u) {
		//System.out.println(u.getUserid());
		this.m = m;
		this.u = u;
		setModal(true);
		/*System.out.println(m.getMovtitle());
		System.out.println(m.getCinename());
		System.out.println(m.getTheater());*/
		al = new BtnActionListener(p);
		setBounds(100, 100, 500, 350);
		setLocationRelativeTo(null);
		setTitle("좌석 선택");
		setResizable(false);
		setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/menu/seat.png")));
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel, BorderLayout.NORTH);
			panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
			{
				JLabel lblNewLabel = new JLabel("S C R E E N");
				lblNewLabel.setFont(new Font("맑은 고딕", Font.BOLD, 24));
				panel.add(lblNewLabel);				
			}			
			{
				JPanel panel2 = new JPanel();
				contentPanel.add(panel2, BorderLayout.WEST);				
				panel2.setLayout(new GridLayout(13, 0, 5, 0));
				
				label = new JLabel[13];
				label[0] = new JLabel();
				panel2.add(label[0]);
				label[0].setIcon(new ImageIcon(S_MovieSeatDialog.class.getResource("/images/seat/blank.png")));
				
				//왼쪽 영어 이미지
				for (int i = 1; i <= 12; ++i) {
					label[i] = new JLabel();
					panel2.add(label[i]);
					label[i].setIcon(new ImageIcon(S_MovieSeatDialog.class.getResource("/images/seat/seatab/" + i + ".png")));
				}

			}
			{
				JPanel panel3 = new JPanel();
				contentPanel.add(panel3, BorderLayout.CENTER);
				panel3.setLayout(new GridLayout(13, 17, 0, 0));
				
				label = new JLabel[18];		
				
				//위 숫자 나열
				for (int i = 1; i < 18; i++) {
					if (i == 5 || i == 13) {	//비울칸을 지정해주고 png 파일을 넣어줌
						label[i] = new JLabel();
						panel3.add(label[i]);
						label[i].setIcon(new ImageIcon(S_MovieSeatDialog.class.getResource("/images/seat/blank.png")));
					} else {
						label[i] = new JLabel();
						panel3.add(label[i]);
						label[i].setIcon(new ImageIcon(S_MovieSeatDialog.class.getResource("/images/seat/" + i + ".png")));
					}
				}
				
				seats = new JCheckBox['M'][18];
				//체크박스 나열
				for (int c = 'A'; c < 'M'; c++) {	//열처리
					for (int i = 1; i < 18; i++) {	//행처리
						if (i == 5 || i == 13) {	//비울칸 지정및 png 파일 넣어줌
							seats[c][i] = new JCheckBox();
							panel3.add(seats[c][i]);
							seats[c][i].setIcon(new ImageIcon(S_MovieSeatDialog.class.getResource("/images/seat/blank.png")));
						} else {							
							seats[c][i] = new JCheckBox();	//비울칸 제외한 곳에 체크박스를 넣어줌
							panel3.add(seats[c][i]);
							seats[c][i].addActionListener(al);
							seats[c][i].setActionCommand("chk");							
							seats[c][i].setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
							seats[c][i].setRolloverIcon(new ImageIcon(S_MovieSeatDialog.class.getResource("/images/seat/seat_rollover.png")));
							seats[c][i].setSelectedIcon(new ImageIcon(S_MovieSeatDialog.class.getResource("/images/seat/seat_rollover.png")));
							seats[c][i].setIcon(new ImageIcon(S_MovieSeatDialog.class.getResource("/images/seat/seat_ok.png")));
						}
					}
				}
			
			}
			
			{
				JPanel buttonPane = new JPanel();
				buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
				getContentPane().add(buttonPane, BorderLayout.SOUTH);
				{
					JButton okButton = new JButton("예매하기");
					okButton.addActionListener(al);						
					okButton.setActionCommand("OK");
					buttonPane.add(okButton);
					getRootPane().setDefaultButton(okButton);					

					List<String> seatinfo =  bd.selectSeat(m);//여기부터 DB 값을 체크박스로 바꿔주기
					//System.out.println(seatinfo.get(10));	
					int cnt = 0;
					for (char c = 'A'; c < 'M'; c++) {
						for (int i = 1; i < 18; ++i) {
							if (i != 5 && i != 13) {
								if (seatinfo.get(cnt).equals("X")) {
									seats[c][i].setSelected(true);									
									seats[c][i].setEnabled(false);
								}
								cnt++;
							}
						}//안 for
					}//바깥 for
					//여기까지				
				}			
				{
					JButton cancelButton = new JButton("취 소");
					cancelButton.addActionListener(al);
					cancelButton.setActionCommand("Cancel");
					buttonPane.add(cancelButton);
				}
			}
		}
		setVisible(true);
		
	}
	
	public class BtnActionListener implements ActionListener {
		private JDialog p = null;
		public BtnActionListener(JDialog p) {
			this.p = p;
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			switch (e.getActionCommand()) {
			case "chk" :				
				as += 1;				
				int people = Integer.parseInt(m.getPeoplenum().substring(0,1));				
				if (as > people) {
					JOptionPane.showMessageDialog(null, m.getPeoplenum() + "의 자리만 선택할 수 있습니다.");
				} else if (as == people) {
					for (char c = 'A'; c < 'M'; c++) {
						for (int i = 1; i < 18; ++i) {
							if (!seats[c][i].isSelected()){
								seats[c][i].setEnabled(false);
							}
						}					
					}
				}
				
				m.setPrice(8000*people + "");
			//	System.out.println(as);
				String seatstr = "";
				
				for (char c = 'A'; c < 'M'; c++) {
					for (int i = 1; i < 18; ++i) {						
						if (i != 5 && i != 13 && seats[c][i].isSelected() && seats[c][i].isEnabled()) {
							if (i > 5 && i < 13) {								
								seatstr += c + "열" + (i-1) + "번  ";
							} else if ( i > 13 && i < 18) {
								seatstr += c + "열" + (i-2) + "번  ";
							} else {
								seatstr += c + "열" + i + "번  ";
							}
							m.setSeat(seatstr);							
						}
					}					
				}
				//System.out.println(m.getSeat());				
				break;
			case "OK" :
				new S_ReservationChkDialog(m);
				break;
			case "OKOK":
				for (char c = 'A'; c < 'M'; c++) {
					for (int i = 1; i < 18; ++i) {
						if (i != 5 && i != 13 && seats[c][i].isSelected()) {	
							isSelec = 'X';
							seats[c][i].setEnabled(false);							
							if (i > 5 && i < 13) {								
								updateSeatSQL = "update seatinfo set " + c + (i-1) + "='" + isSelec + "' where showno ="+ bd.selectShowno(m);											
								bd.updateSeat(updateSeatSQL);
							} else if ( i > 13 && i < 18) {
								updateSeatSQL = "update seatinfo set " + c + (i-2) + "='" + isSelec + "' where showno ="+ bd.selectShowno(m);											
								bd.updateSeat(updateSeatSQL);
							} else {
								updateSeatSQL = "update seatinfo set " + c + i + "='" + isSelec + "' where showno = "+ bd.selectShowno(m);											
								bd.updateSeat(updateSeatSQL);
							}
						}
					}//안 for
				}//바깥 for ㄲ
				
				
				//System.out.println(m.getPrice());
				bd.insertReservation(m, u);
				
				as = 0;			
				
				JOptionPane.showMessageDialog(null, "예매 되었습니다.");
				
				setVisible(false);
				dispose();
				break;
			case "Cancel" :
				as = 0;
				setVisible(false);
				dispose();
				break;				
			}			
		}		
	}	
}


